package netty.websocket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class HeartBeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof IdleStateEvent){
            IdleStateEvent idleStateEvent=(IdleStateEvent) evt;
            if(idleStateEvent.state()== IdleState.WRITER_IDLE){
                System.out.println("读空闲");
            }
            else if(idleStateEvent.state()==IdleState.WRITER_IDLE){
                System.out.println("写空闲");
            }
            else if(idleStateEvent.state()==IdleState.ALL_IDLE){
                System.out.println("读写空闲，关闭channel");
                ctx.channel().close();
            }
        }
    }

}
